﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace HYRElectric.WirePlace
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class ConnectWire : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Document document = activeUIDocument.ActiveView.Document;
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((HYView.CmdViewType)3, cmdData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			using (WireParamNon_modeForm wireParamNon_modeForm = new WireParamNon_modeForm(document, true))
			{
				wireParamNon_modeForm.Show(WindowHelper.HWndRevit);
				WindowHelper.SendMessageToWndRevit();
				for (;;)
				{
					try
					{
						if (wireParamNon_modeForm.isClose)
						{
							break;
						}
						NewConnectWire.CreateWireBySelection(cmdData, activeUIDocument);
					}
					catch (Exception)
					{
						if (NewConnectWire.isFirstEquipment)
						{
							wireParamNon_modeForm.Close();
							break;
						}
						if (wireParamNon_modeForm.isClose)
						{
							break;
						}
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
